package com.he.springcloud;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;

/**
 * @Classname: hystrixPaymentMain8001
 * @Description: 熔断的启动类
 *                  使用Hystrix要在主启动类上加@EnableCircuitBreaker注解
 * @Date: 2021/9/8
 * @Author: huwei
 */
@EnableEurekaClient
@Slf4j
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableCircuitBreaker
public class HystrixPaymentMain8001 {
    public static void main(String[] args) {
        SpringApplication.run(HystrixPaymentMain8001.class,args);
        log.info("hystrix启动成功");
    }

    /**
     *
     * 服务监控配置(非必要) 如果出现Unable to connect to Command Metric Stream错误,就加入此配置
     *  此配置和springboot无关,ServletRegistrationBean容器在springboot中默认路径不是"/hystrix.stream"
     *  只要在自己的项目里配置下servlet就可以了
     */
    @Bean
    public ServletRegistrationBean getServlet(){
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/actuator/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}
